//
//  OMOrderSubmitGoodListTableCell.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/11.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMOrderSubmitGoodListTableCell.h"
#import "OMOrderBookResponse.h"
#import <YYText.h>
#import "OMOrderSubmitProductTableCell.h"
#import "OMOrder.h"

#define OMOrderSubCellHeight SM_SIZE(140)

@interface OMOrderSubmitGoodListTableCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, weak) OMOrder *order;
@end

@implementation OMOrderSubmitGoodListTableCell

- (void)configureCellWithObj:(OMOrderBookResponse *)response
{
    self.order = response.order;

    self.priceTotalDetailLabel.text = [[NSString stringWithFormat:@"￥%.2f",response.order.price.floatValue]  stringByReplacingOccurrencesOfString:@".00" withString:@""];
    self.deliveryFeeDetailLabel.text = [[NSString stringWithFormat:@"￥%.2f",response.order.freight.floatValue]  stringByReplacingOccurrencesOfString:@".00" withString:@""];
    self.couponDetailLabel.text = [[NSString stringWithFormat:@"-￥%.2f",response.order.couponDiscount+response.order.promotionDiscount]  stringByReplacingOccurrencesOfString:@".00" withString:@""];
    self.amountDetailLabel.text = [[NSString stringWithFormat:@"￥%.2f",response.order.amount.floatValue-response.order.couponDiscount] stringByReplacingOccurrencesOfString:@".00" withString:@""];
}

- (void)setOrder:(OMOrder *)order
{
    _order = order;
    //商品列表
    [self.tableView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@((OMOrderSubCellHeight*order.orderItems.count)));
    }];
    [self.tableView reloadData];
}

- (void)configure
{
    [super configure];

    UIView *topLine = [UIView new];
    topLine.backgroundColor = SM_naviShadowColor;
    [self.contentView addSubview:topLine];
    [topLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.height.equalTo(@.5f);
    }];

    [self.contentView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@0);
        make.top.equalTo(topLine.mas_bottom).offset(SM_SIZE(0));
        make.height.equalTo(@0);
    }];


    UIView *separatorLine = [UIView new];
//    separatorLine.backgroundColor = SM_naviShadowColor;
    [self.contentView addSubview:separatorLine];
    [separatorLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(20)));
        make.right.equalTo(@(SM_SIZE(-20)));
        make.height.equalTo(@0.5f);
        make.top.equalTo(self.tableView.mas_bottom).offset(SM_SIZE(0));
    }];


    _priceTotalLabel = [UILabel new];
    _priceTotalLabel.font = SM_CFont(29);
    _priceTotalLabel.textColor = SM_darkGrayColor;
    _priceTotalLabel.text = @"商品总价";
    [self.contentView addSubview:_priceTotalLabel];
    [_priceTotalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(20)));
        make.top.equalTo(separatorLine.mas_bottom).offset(SM_SIZE(30));
    }];


    _deliveryFeeLabel = [UILabel new];
    _deliveryFeeLabel.font = _priceTotalLabel.font;
    _deliveryFeeLabel.textColor = SM_darkGrayColor;
    _deliveryFeeLabel.text = @"配送费";
    [self.contentView addSubview:_deliveryFeeLabel];
    [_deliveryFeeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.priceTotalLabel.mas_left);
        make.top.equalTo(self.priceTotalLabel.mas_bottom).offset(SM_SIZE(20));
    }];

    _couponLabel = [UILabel new];
    _couponLabel.font = self.priceTotalLabel.font;
    _couponLabel.textColor = self.priceTotalLabel.textColor;
    _couponLabel.text = @"优惠";
    [self.contentView addSubview:_couponLabel];
    [_couponLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.priceTotalLabel.mas_left);
        make.top.equalTo(self.deliveryFeeLabel.mas_bottom).offset(SM_SIZE(20));
    }];


    _amountLabel = [UILabel new];
    _amountLabel.font = self.priceTotalLabel.font;
    _amountLabel.textColor = self.priceTotalLabel.textColor;
    _amountLabel.text = @"应付";
    [self.contentView addSubview:_amountLabel];
    [_amountLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.priceTotalLabel.mas_left);
        make.top.equalTo(self.couponLabel.mas_bottom).offset(SM_SIZE(20));
        make.bottom.equalTo(@(SM_SIZE(-40)));
    }];


    _priceTotalDetailLabel = [UILabel new];
    _priceTotalDetailLabel.textColor = SM_blackColor;
    _priceTotalDetailLabel.font = SM_NFont(29);
    _priceTotalDetailLabel.text = @"￥6";
    [self.contentView addSubview:_priceTotalDetailLabel];
    [_priceTotalDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@(SM_SIZE(-20)));
        make.top.equalTo(self.priceTotalLabel.mas_top);
    }];


    _deliveryFeeDetailLabel = [UILabel new];
    _deliveryFeeDetailLabel.textColor = _priceTotalDetailLabel.textColor;
    _deliveryFeeDetailLabel.font = _priceTotalDetailLabel.font;
    _deliveryFeeDetailLabel.text = @"￥5";
    [self.contentView addSubview:_deliveryFeeDetailLabel];
    [_deliveryFeeDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.priceTotalDetailLabel.mas_right);
        make.top.equalTo(self.priceTotalDetailLabel.mas_bottom).offset(SM_SIZE(20));
    }];


    _couponDetailLabel = [UILabel new];
    _couponDetailLabel.font = self.deliveryFeeDetailLabel.font;
    _couponDetailLabel.textColor = SM_redColor;
    _couponDetailLabel.text = @"-￥0";
    [self.contentView addSubview:_couponDetailLabel];
    [_couponDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.deliveryFeeDetailLabel.mas_right);
        make.top.equalTo(self.deliveryFeeDetailLabel.mas_bottom).offset(SM_SIZE(20));
    }];

    _amountDetailLabel = [UILabel new];
    _amountDetailLabel.font = SM_NFont(32);
    _amountDetailLabel.textColor = self.deliveryFeeDetailLabel.textColor;
    _amountDetailLabel.text = @"￥11.0";
    [self.contentView addSubview:_amountDetailLabel];
    [_amountDetailLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.deliveryFeeDetailLabel.mas_right);
        make.top.equalTo(self.amountLabel.mas_top);
    }];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.order.orderItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    OMOrderSubmitProductTableCell *cell = [OMOrderSubmitProductTableCell tableViewCellWithTableView:tableView];
    [cell configureCellWithObj:self.order.orderItems[indexPath.row]];
    return cell;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return OMOrderSubCellHeight;
}

#pragma mark - Getters
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.backgroundColor = SM_whiteColor;
        _tableView.separatorColor = SM_naviShadowColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.sectionHeaderHeight = DBL_EPSILON;
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 1, DBL_EPSILON)];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        _tableView.separatorColor = SM_naviShadowColor;
        _tableView.scrollEnabled = false;
    }
    return _tableView;
}
@end
